package com.zenitera.bigdata.project01;

import com.zenitera.bigdata.bean.UserBehavior;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;
import java.util.Set;

/**
 * 样例数据
 * userId itemId categoryId behavior timestamp
 * 543462,1715,1464116,pv,1511658000
 */
public class Flink02_Project01_UV {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.prot", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env
                .readTextFile("input/UserBehavior.csv")
                .map(line -> {
                    String[] data = line.split(",");
                    return new UserBehavior(
                            Long.valueOf(data[0]),
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2]),
                            String.valueOf(data[3]),
                            Long.valueOf(data[4])
                    );
                })
                .filter(ub -> "pv".equals(ub.getBehavior()))
                .keyBy(UserBehavior::getBehavior)
                .process(new KeyedProcessFunction<String, UserBehavior, String>() {
                             Set<Long> userIdSet = new HashSet<>();

                             @Override
                             public void processElement(UserBehavior value,
                                                        Context ctx,
                                                        Collector<String> out) throws Exception {
                                 if (userIdSet.add(value.getUserId())) {
                                     out.collect("uv当前值: " + userIdSet.size());
                                 }
                             }
                         }

                )

                .print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
